Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
How to dump http "Content-type: application/json;" in FastAPI

I will write a python script that listen to a webhook for a custom tool that will send json(They might support other format also) on a port I can specify.

How to write something similar to linux command: "nc -l 9000" to dump the out put I get on that port (header and body)?

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    print()  <- how to get the data here?
    return {"message": "ok"}

I want to print the content in the terminal, then I can easy see what I will get and take action on it. Not sure what I should replay to them if that is even needed (need to check this, they are not done with their part yet).

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

So i think you are looking for Request.

This contains a lot of information (including, Body, Form, Headers etc.) about the request.

from fastapi import FastAPI, Request

app = FastAPI()


@app.get("/")
def read_root(request: Request):
    print(request.headers)
    return {}

So now if i send a request to this endpoint this 'll return me

{
    "host":"127.0.0.1:8000",
    "connection":"keep-alive",
    "accept":"application/json",
    "sec-fetch-site":"same-origin",
    "sec-fetch-mode":"cors",
    "sec-fetch-dest":"empty",
    "referer":"http://127.0.0.1:8000/docs",
    "accept-encoding":"gzip, deflate, br",
    "accept-language":"en-US,en;q=0.9,tr;q=0.8",
    "cookie":"csrftoken=sdf6ty78uewfıfehq7y8fuq; _ga=GA.1.11242141,1234423"
}

Then you can extract the accept from request.headers

If you only need that, you can also do

@app.get("/")
def read_root(request: Request):
    print(request.headers['accept'])
    return {}

This will return only

Out: application/json
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!